<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="数据年份" prop="year">
        <el-date-picker v-model="queryParams.year" type="year" value-format="YYYY" placeholder="请选择数据年份" clearable
          style="width: 240px" />
      </el-form-item>
      <el-form-item label="责任人" prop="taskLeaderId">
        <el-select v-model="queryParams.taskLeaderId" placeholder="请选择责任人" clearable style="width: 240px">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="任务类型" prop="taskType">
        <el-select v-model="queryParams.taskType" placeholder="请选择任务类型" clearable style="width: 240px">
          <el-option v-for="dict in task_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="查询计划时间段" style="width: 308px">
        <el-date-picker v-model="daterangeScheduledStartTime" value-format="YYYY-MM-DD" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 308px" />
      </el-form-item>
      <el-form-item label="查询执行时间段" style="width: 308px">
        <el-date-picker v-model="daterangeActualStartTime" value-format="YYYY-MM-DD" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 308px" />
      </el-form-item>
      <el-form-item label="关联地块" prop="plantPlotId">
        <el-select v-model="queryParams.plantPlotId" placeholder="请选择关联地块" clearable style="width: 240px">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="种植主体" prop="planterId">
        <el-select v-model="queryParams.planterId" placeholder="请选择种植主体" clearable style="width: 240px">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="产区" prop="productAreaId">
        <el-select v-model="queryParams.productAreaId" placeholder="请选择产区" clearable style="width: 240px">
          <el-option v-for="dict in product_area" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否按时完成" prop="isOntime">
        <el-radio-group v-model="form.isOntime">
          <el-radio v-for="dict in biz_yes_no" :key="dict.value" :value="dict.value">
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['task:task:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['task:task:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['task:task:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['task:task:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="任务类型" align="center" prop="taskType">
        <template #default="scope">
          <dict-tag :options="task_type" :value="scope.row.taskType" />
        </template>
      </el-table-column>
      <el-table-column label="责任人" align="center" prop="taskLeader" />
      <el-table-column label="计划开始时间" align="center" prop="scheduledStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.scheduledStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际开始时间" align="center" prop="actualStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.actualStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="关联地块" align="center" prop="plantPlotName" />
      <el-table-column label="种植主体" align="center" prop="planterName" />
      <el-table-column label="产区" align="center" prop="productAreaName" />
      <el-table-column label="数据年份" align="center" prop="year" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.year, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否按时完成" align="center" prop="isOntime">
        <template #default="scope">
          <dict-tag :options="biz_yes_no" :value="scope.row.isOntime" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['task:task:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['task:task:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改种植任务明细对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="taskRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-if="renderField(true, true)" label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="责任人" prop="taskLeaderId">
          <el-select v-model="form.taskLeaderId" placeholder="请选择责任人">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="任务类型" prop="taskType">
          <el-select v-model="form.taskType" placeholder="请选择任务类型">
            <el-option v-for="dict in task_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="renderField(true, false)" label="责任人" prop="taskLeader">
          <el-input v-model="form.taskLeader" placeholder="请输入责任人" />
        </el-form-item>
        <el-form-item v-if="renderField(true, false)" label="计划开始时间" prop="scheduledStartTime">
          <el-date-picker clearable v-model="form.scheduledStartTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择计划开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="renderField(true, false)" label="计划结束时间" prop="scheduledEndTime">
          <el-date-picker clearable v-model="form.scheduledEndTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择计划结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="renderField(false, true)" label="实际开始时间" prop="actualStartTime">
          <el-date-picker clearable v-model="form.actualStartTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择实际开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="renderField(false, true)" label="实际结束时间" prop="actualEndTime">
          <el-date-picker clearable v-model="form.actualEndTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择实际结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="任务详情" prop="taskDescription">
          <editor v-model="form.taskDescription" :min-height="192" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="关联地块" prop="plantPlotId">
          <el-select v-model="form.plantPlotId" placeholder="请选择关联地块">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="关联地块" prop="plantPlotName">
          <el-input v-model="form.plantPlotName" placeholder="请输入关联地块" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="种植主体Id" prop="planterId">
          <el-select v-model="form.planterId" placeholder="请选择种植主体Id">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="种植主体" prop="planterName">
          <el-input v-model="form.planterName" placeholder="请输入种植主体" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="产区" prop="productAreaId">
          <el-select v-model="form.productAreaId" placeholder="请选择产区">
            <el-option v-for="dict in product_area" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="产区" prop="productAreaName">
          <el-input v-model="form.productAreaName" placeholder="请输入产区" />
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="数据年份" prop="year">
          <el-date-picker clearable v-model="form.year" type="date" value-format="YYYY-MM-DD" placeholder="请选择数据年份">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="renderField(true, true)" label="是否按时完成" prop="isOntime">
          <el-radio-group v-model="form.isOntime">
            <el-radio v-for="dict in biz_yes_no" :key="dict.value" :value="dict.value">{{
              dict.label
            }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Task">
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/plant/task";

const { proxy } = getCurrentInstance();
const { product_area, task_type, biz_yes_no } = proxy.useDict('product_area', 'task_type', 'biz_yes_no');

const taskList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeScheduledStartTime = ref([]);
const daterangeActualStartTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    taskLeaderId: null,
    taskType: null,
    scheduledStartTime: null,
    actualStartTime: null,
    plantPlotId: null,
    planterId: null,
    productAreaId: null,
    year: null,
    isOntime: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询种植任务明细列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  queryParams.value.params = {};
  if (null != daterangeScheduledStartTime && '' != daterangeScheduledStartTime) {
    queryParams.value.beginScheduledStartTime = daterangeScheduledStartTime.value[0];
    queryParams.value.endScheduledStartTime = daterangeScheduledStartTime.value[1];
  }
  if (null != daterangeActualStartTime && '' != daterangeActualStartTime) {
    queryParams.value.beginActualStartTime = daterangeActualStartTime.value[0];
    queryParams.value.endActualStartTime = daterangeActualStartTime.value[1];
  }
  listTask(queryParams.value).then(response => {
    taskList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    taskId: null,
    taskName: null,
    taskLeaderId: null,
    taskType: null,
    taskLeader: null,
    scheduledStartTime: null,
    scheduledEndTime: null,
    actualStartTime: null,
    actualEndTime: null,
    taskDescription: null,
    plantPlotId: null,
    plantPlotName: null,
    planterId: null,
    planterName: null,
    productAreaId: null,
    productAreaName: null,
    year: null,
    isOntime: null,
  };
  proxy.resetForm("taskRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeScheduledStartTime.value = [];
  daterangeActualStartTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.taskId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加种植任务明细";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _taskId = row.taskId || ids.value;
  getTask(_taskId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改种植任务明细";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["taskRef"].validate(valid => {
    if (valid) {
      if (form.value.taskId != null) {
        updateTask(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTask(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _taskIds = row.taskId || ids.value;
  proxy.$modal.confirm('是否确认删除种植任务明细编号为"' + _taskIds + '"的数据项？').then(function () {
    return delTask(_taskIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('task/task/export', {
    ...queryParams.value
  }, `task_${new Date().getTime()}.xlsx`);
}

/** 是否渲染字段 */
function renderField(insert, edit) {
  return form.value.taskId == null ? insert : edit;
}

getList();
</script>